﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Reflection;

namespace Cashier_DAO.db
{
    public class DbConfig
    {
        /// <summary>
        /// 数据库类型
        /// </summary>
        public static String Dbtype;

        /// <summary>
        /// 数据库名称
        /// </summary>
        public static String Dbname;

        /// <summary>
        /// 服务主机地址
        /// </summary>
        public static String Host;

        /// <summary>
        /// 数据库端口号
        /// </summary>
        public static String Port;

        /// <summary>
        /// 驱动名称
        /// </summary>
        public static String Driver;

        /// <summary>
        /// 用户名
        /// </summary>
        public static String Username;

        /// <summary>
        /// 密码
        /// </summary>
        public static String Password;

        static DbConfig()
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                XmlElement root = null;
                FileInfo file = new FileInfo(System.Windows.Forms.Application.StartupPath + @"\config.xml");
                if (file.Exists)
                {                   
                    doc.Load(file.FullName);
                    root = doc.DocumentElement;
                }
                else
                {
                    throw new Exception("ERROR:config file is not exist!");
                }

                if (root != null)
                {
                    XmlNode node = root.SelectSingleNode("/config/setting/database/dbtype");
                    if (node != null)
                        Dbtype = node.InnerText;

                    node = root.SelectSingleNode("/config/setting/database/dbname");
                    if (node != null)
                        Dbname = node.InnerText;

                    node = root.SelectSingleNode("/config/setting/database/host");
                    if (node != null)
                        Host = node.InnerText;

                    node = root.SelectSingleNode("/config/setting/database/port");
                    if (node != null)
                        Port = node.InnerText;

                    node = root.SelectSingleNode("/config/setting/database/driver");
                    if (node != null)
                        Driver = node.InnerText;

                    node = root.SelectSingleNode("/config/setting/database/username");
                    if (node != null)
                        Username = node.InnerText;

                    node = root.SelectSingleNode("/config/setting/database/password");
                    if (node != null)
                        Password = node.InnerText;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
